package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Notice;
import com.example.mapper.NoticeMapper;
import com.example.service.NoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/11/8 12:06
 * @Version 1.0.0
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 新增
     *
     * @param notice
     */
    @Override
    public void add(Notice notice) {
        notice.setTime(DateUtil.today());
        noticeMapper.insert(notice);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        noticeMapper.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            noticeMapper.deleteById(id);
        }
    }

    /**
     * 修改
     *
     * @param notice
     */
    @Override
    public void updateNotice(Notice notice) {
        noticeMapper.updateById(notice);
    }

    /**
     * 根据ID查询
     *
     * @param id
     */
    @Override
    public Notice selectById(Integer id) {
        return noticeMapper.selectById(id);
    }

    /**
     * 查询所有
     *
     * @param notice
     */
    @Override
    public List<Notice> selectAll(Notice notice) {
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(notice.getTitle()),Notice::getTitle,notice.getTitle());
        return noticeMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 分页查询
     *
     * @param notice
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<Notice> selectPage(Notice notice, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Notice> list = selectAll(notice);
        return PageInfo.of(list);
    }
}
